package com.estronger.shopmanage.adapter.tasking;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.DeliverDetail;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.ConvertUtils;


public class DeliverDetailAdpter extends BaseAdapter {

    private DeliverDetail mList;
    Context mContext;

    public DeliverDetailAdpter(Context context, DeliverDetail lt) {
        mContext = context;
        mList = lt;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.data.size();
        else
            return 0;
    }

    @Override
    public DeliverDetail getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.data.size() <= 0)
            return null;
        else
            return mList;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView ltv;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_deliver_detail, null);
            ltv = new ListItemView();
            ltv.spname = (TextView) convertView.findViewById(R.id.spname);
            ltv.spguige = (TextView) convertView.findViewById(R.id.spguige);
            ltv.spshuliang = (TextView) convertView.findViewById(R.id.spshuliang);
            ltv.sppic = (ImageView) convertView.findViewById(R.id.sppic);
            ltv.danwei = (TextView) convertView.findViewById(R.id.goods_feature);

            convertView.setTag(ltv);
        } else {
            ltv = (ListItemView) convertView.getTag();
        }

        try {
            ltv.spname.setText(ConvertUtils.formatNull(mList.data.get(position).goodsname));
            ltv.spguige.setText(ConvertUtils.formatNull(mList.data.get(position).sizedesc));
            ltv.spshuliang.setText(ConvertUtils.formatNull(mList.data.get(position).qty));
            ltv.danwei.setText(ConvertUtils.formatNull(mList.data.get(position).unitname));

            //取得的路径用Glide来加载
            String picpath = mList.data.get(position).simplepicfile;
            if (picpath != null) {
                if (picpath != null && !picpath.equals("") && !picpath.equals("null")) {
                    MyApplication.imageLoader.displayImage(NetManager.getDownloadURL() + "?filepath=" + picpath, ltv.sppic, MyApplication.options);
                } else {
                    ltv.sppic.setImageResource(R.drawable.no_picture_icon_b);
                }
//            Glide.with(mContext)
//                    .load(picpath)
//                    .placeholder(R.mipmap.no_picture_icon_b)
//                    .error(R.mipmap.no_picture_icon_b)
//                    .fitCenter()
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .into(groupHolder.img_spimg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    class ListItemView {
        TextView spname;
        TextView spguige;
        TextView spshuliang;
        ImageView sppic;
        TextView danwei;
    }
}
